package com.leyou.common.advice;


import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import lombok.With;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class ComemomLognAdvice {
    @Around("within(@org.springframework.stereotype.Service *)" +
            "|| (within(com.baomidou.mybatisplus.extension.service.IService+) " +
            "&& ! execution(* com.baomidou.mybatisplus.extension.service.IService.query())" +
            "&& ! execution(* com.baomidou.mybatisplus.extension.service.IService.update())" +
            ")")
    public Object handleExceptionLog(ProceedingJoinPoint jp){
        //记录方法参数
        String name = jp.getSignature().getName();
        log.debug("方法{}正在被调用，入参：[{}]",name, Arrays.toString(jp.getArgs()));
        Object obj = null;
        try {
            //放行切点拦截到的service的方法
            obj = jp.proceed();
            log.debug("方法{}调用完成",name);
            //返回结果
            return obj;
        } catch (Throwable throwable) {
            //判断是否是LyException如果是跳过
            if (throwable instanceof LyException){
                log.debug("方法{}调用错误，原因：{}",name,throwable.getMessage()  );
                throw (LyException) throwable;
            }
            log.error(throwable.getMessage(), throwable);
            throw new RuntimeException(throwable);
        }
    }
}
